<?php

/**
 * This file is part of the Framework03 PHP framework.
 * @author Daniel de Vries
 * @package Framework03
 */

/**
 * @brief This class will act as a logger so that messages get written to their destination.
 */
class FW_Log
{
	private static $pathToLog = null;
	private static $prependDate = true;

    /**
     * Same use as sprintf, except this is logged in to a log file
     * @see http://www.php.net/sprintf
     * @param string $format
     * @return the formatted string
     */
    public static function write($format)
    {
        $arguments = func_get_args();
        array_shift($arguments); // we dont need the first argument
        $message = vsprintf($format, $arguments) . "\n";

        if (self::$prependDate == true)
            $message = date('[d-m-Y H:m:s]') . " - " . $message;

        error_log($message, 3, self::$pathToLog);
        return $message;
    }

    public static function setLogFile($pathToLog)
    {
		if (!is_file($pathToLog) && !touch($pathToLog))
		{
			throw new FW_Log_Exception("'$pathToLog' is not writable");
		}
        if (!is_writable($pathToLog))
        {
            throw new FW_Log_Exception("'$pathToLog' is not writable");
        }
		
        self::$pathToLog = $pathToLog;
    }

    public static function setPrependDate($bool)
    {
        self::$prependDate = $bool;
    }
}
